package com.fengli.task.uitl.exceptionHandler;

import com.fengli.task.uitl.Result;
import com.fengli.task.uitl.enumType.Code;
import com.fengli.task.uitl.exception.CanNotFindDataException;
import com.fengli.task.uitl.exception.ParamException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;

/**
 * @Description: 异常处理
 * @Author: fengli
 * @Date 4/19/19 9:35 AM
 */
@ControllerAdvice
@ResponseBody
public class ExceptionHandlerAdvice {
    @ExceptionHandler(ParamException.class)
    public Result handleIllegalParamException(Exception e) {
        Result result = new Result(e.getMessage(), Code.FAIL,"");
        return result;
    }

    /**
     * 从数据库找不到数据
     * @param e
     * @return
     */
    @ExceptionHandler(CanNotFindDataException.class)
    public Result handleCanNotFindDataException(Exception e) {
        Result result = new Result(e.getMessage(), Code.FAIL,"");
        return result;
    }

    @ExceptionHandler(SQLException.class)
    public Result handleSQLException(Exception e){
        Result result = new Result(e.getMessage(), Code.FAIL,"");
        return result;
    }
}
